package org.dgw;

import org.dgw.domain.Person;
import org.dgw.utils.JPAUtil;
import org.hibernate.Transaction;
import org.junit.Test;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;

/**
 * @program: Jpa-01
 * @description:
 * @author: Mr.Dai
 * @create: 2020-01-01 17:16
 **/
public class JpaCrud {

    @Test
    public void insert(){
        EntityManager manager=null;
        EntityTransaction transaction =null;
        try {
            manager = JPAUtil.getEntityManager();
            transaction = manager.getTransaction();
            Person person = new Person();
            person.setLastName("admin1");
            person.setEmail("123@qq.com");
            person.setBirth(new Date());
            transaction.begin();
            manager.persist(person);
            transaction.commit();
        }catch (Exception e){
            transaction.rollback();
            e.printStackTrace();
        }finally {
            manager.close();
        }
    }

    @Test
    public void delete(){
        EntityManager manager=null;
        EntityTransaction transaction =null;
        try {
            manager = JPAUtil.getEntityManager();
            transaction = manager.getTransaction();
            Person person = new Person();
            person.setLastName("admin1");
            person.setEmail("123@qq.com");
            person.setBirth(new Date());
            transaction.begin();

            Person person1 = manager.find(Person.class, 1);
            manager.remove(person1);

            transaction.commit();
        }catch (Exception e){
            transaction.rollback();
            e.printStackTrace();
        }finally {
            manager.close();
        }
    }
    @Test
    public void pageStartingSele(){
        EntityManager manager=null;
        EntityTransaction transaction =null;
        try {
            manager = JPAUtil.getEntityManager();
            transaction = manager.getTransaction();
            transaction.begin();

            Query query = manager.createQuery("select p from Person p ");
            // 起始页
            query.setFirstResult(0);
            // 每一页5个
            query.setMaxResults(5);

            List list = query.getResultList();
            for (Object list1 :list){
                System.out.println(list1);
            }
            transaction.commit();
        }catch (Exception e){
            transaction.rollback();
            e.printStackTrace();
        }finally {
            manager.close();
        }
    }
    @Test
    public void findCondition(){
        EntityManager manager=null;
        EntityTransaction transaction =null;
        try {
            manager = JPAUtil.getEntityManager();
            transaction = manager.getTransaction();
            transaction.begin();

            Query query = manager.createQuery("from Person p where p.lastName like :name");
            //query.setParameter(1,"m");
            query.setParameter("name","%m");
            List list = query.getResultList();
            System.out.println(list);
            transaction.commit();
        }catch (Exception e){
            transaction.rollback();
            e.printStackTrace();
        }finally {
            manager.close();
        }
    }
}
